<template>
  <div class="patient-detail-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <el-page-header @back="goBack">
            <template #content>
              <span>{{ patientForm.id ? '修改' : '新增' }}就诊人</span>
            </template>
          </el-page-header>
        </div>
      </template>
      <div class="container">
        <el-divider content-position="left">就诊人信息</el-divider>
        <el-form
          ref="addForm"
          :model="patientForm"
          label-width="100px"
          :rules="FormRule"
          label-position="left">
          <el-form-item label="用户姓名" prop="name">
            <el-input v-model="patientForm.name" placeholder="请输入用户姓名" />
          </el-form-item>
          <el-form-item label="证件类型" prop="certificatesType">
            <el-select clearable placeholder="选择证件类型" v-model="patientForm.certificatesType">
              <el-option
                :label="item.name"
                v-for="item in certificateType"
                :key="item.id"
                :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="证件号码" prop="certificatesNo">
            <el-input placeholder="请输入证件号码" v-model="patientForm.certificatesNo" />
          </el-form-item>
          <el-form-item label="用户性别" prop="sex">
            <el-radio v-model="patientForm.sex" :label="0">男</el-radio>
            <el-radio v-model="patientForm.sex" :label="1">女</el-radio>
          </el-form-item>
          <el-form-item label="出生日期" prop="birthdate">
            <el-date-picker
              v-model="patientForm.birthdate"
              style="width: 100%"
              value-format="YYYY-MM-DD"
              type="date"
              placeholder="出生日期" />
          </el-form-item>
          <el-form-item label="手机号码" prop="phone">
            <el-input placeholder="请输入手机号码" v-model="patientForm.phone" />
          </el-form-item>
        </el-form>
        <el-divider content-position="left">建档信息(完善后部分医院首次就诊不排队建档)</el-divider>
        <el-form
          ref="addForm1"
          :model="patientForm"
          :rules="FormRule"
          label-width="100px"
          label-position="left">
          <el-form-item label="婚姻状况" prop="isMarry">
            <el-radio v-model="patientForm.isMarry" :label="0">已婚</el-radio>
            <el-radio v-model="patientForm.isMarry" :label="1">未婚</el-radio>
          </el-form-item>
          <el-form-item label="自费/医保" prop="isInsure">
            <el-radio v-model="patientForm.isInsure" :label="0">自费</el-radio>
            <el-radio v-model="patientForm.isInsure" :label="1">医保</el-radio>
          </el-form-item>
          <el-form-item label="当前住址" prop="address">
            <el-cascader
              v-model="patientForm.addressSelected"
              :props="props"
              clearable
              style="width: 100%"
              :options="proviseOptions" />
          </el-form-item>
          <el-form-item label="详细住址" prop="address">
            <el-input placeholder="请输入详细住址" v-model="patientForm.address" />
          </el-form-item>
        </el-form>
        <el-divider content-position="left">联系人信息(选填)</el-divider>
        <el-form :model="patientForm" label-width="100px" label-position="left">
          <el-form-item label="联系人姓名" prop="contactsName">
            <el-input placeholder="请输入联系人姓名" v-model="patientForm.contactsName" />
          </el-form-item>
          <el-form-item label="证件类型" prop="contactsCertificatesType">
            <el-select
              clearable
              placeholder="选择证件类型"
              v-model="patientForm.contactsCertificatesType">
              <el-option
                :label="item.name"
                v-for="item in certificateType"
                :key="item.id"
                :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="证件号码" prop="contactsCertificatesNo">
            <el-input placeholder="请输入证件号码" v-model="patientForm.contactsCertificatesNo" />
          </el-form-item>
          <el-form-item label="手机号码" prop="contactsPhone">
            <el-input placeholder="请输入手机号码" v-model="patientForm.contactsPhone" />
          </el-form-item>
          <el-form-item>
            <div class="btn-div">
              <el-button type="primary" @click="handleSave">提交</el-button>
              <el-button type="info" plain>重写</el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { Patient } from '@/api/patient/type'
import { reqUpdateOrSavePatientInfo } from '@/api/patient'
import useDictStore from '@/store/modules/dict'
import { reqGetAddressByParentId } from '@/api/user'
import { Address } from '@/api/user/type'
import FormRule from './rules.ts'
import { ElMessage } from 'element-plus'

let $emit = defineEmits(['handleSaveSuccess', 'handleGoback'])
let patientForm = ref({} as Patient)
const dictStore = useDictStore()
let certificateType = ref<any>([])
// 表单初始化
const resetData = () => {
  patientForm.value = {
    sex: 1
  } as Patient
}

const props = {
  label: 'name',
  value: 'value',
  lazy: true,
  lazyLoad: async (node, resolve) => {
    console.log(node.id)
    const { data } = await reqGetAddressByParentId(node.value || 86)
    data.forEach((item: Address) => {
      item.leaf = !item.hasChildren
    })
    resolve(data)
  }
}
let proviseOptions = ref<Address[]>([])
onMounted(async () => {
  await dictStore.getCertificateType()
  certificateType.value = dictStore.certificateType
})
// 初始化赋值
const initData = (patient: Patient) => {
  addForm.value.clearValidate()
  addForm1.value.clearValidate()
  patientForm.value = patient
  if (patient.id) {
    patientForm.value.addressSelected = [
      patient.provinceCode,
      patient.cityCode,
      patient.districtCode
    ]
  }
}
let addForm = ref()
let addForm1 = ref()
const handleSave = async () => {
  await addForm.value.validate()
  await addForm1.value.validate()
  if (patientForm.value.addressSelected) {
    patientForm.value.provinceCode = patientForm.value.addressSelected[0]
    patientForm.value.cityCode = patientForm.value.addressSelected[1]
    patientForm.value.districtCode = patientForm.value.addressSelected[2]
  }
  await reqUpdateOrSavePatientInfo(patientForm.value)
  ElMessage.success('保存成功')
  $emit('handleSaveSuccess')
}
const goBack = () => {
  // 跳转到添加就诊人页面
  $emit('handleGoback')
}
defineExpose({ initData, resetData })
</script>

<style lang="scss" scoped>
.patient-detail-container {
  width: 100%;
  height: 100%;
  .el-form {
    width: 60%;
    margin: 0 auto;
  }
}
</style>
